package com.ym.b2b.brand.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.brand.dao.EmBBrandDAO;
import com.ym.b2b.brand.service.EmBBrandService;
import com.ym.b2b.brand.sqlbean.EmBBrand;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmBBrandServiceImpl
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-3-6 下午4:40:21
 * @Modify
 * @CopyRight B2B小组
 */
@Service("emBBrandService")
public class EmBBrandServiceImpl extends BaseService implements EmBBrandService {

    private Logger      logger = Logger.getLogger(getClass());

    @Autowired
    private EmBBrandDAO brandDao;

    private Logger         dbLog = Logger.getLogger("db");
    
    @Override
    public Result searchPageEmBBrand(Page<EmBBrand> page, EmBBrand emBBrand) throws DataAccessException {
        Result result = new Result();

        try {
            result.setResultPage(brandDao.searchPageEmBBrand(emBBrand, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    @Override
    public Result showEmBBrandDetail(EmBBrand emBBrand) throws DataAccessException {
        Result result = new Result();
        try {
            // 品牌详情
            emBBrand = brandDao.showEmBBrandDetail(emBBrand);
            result.putData("emBBrand", emBBrand);
            result.setResult("获取品牌详情成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "获取品牌详情失败!");
        }
        return result;
    }

    @Override
    public Result insertEmBBrand(EmBBrand emBBrand) throws DataAccessException {
        emBBrand.setIsShow(1);
        Result result = this.checkInputEmBBrand(emBBrand);
        if (!result.getCode().equals("200")) return result;
        try {
            brandDao.insertEmBBrand(emBBrand);
            dbLog.info("INSERT-->EM_B_BRAND-品牌ID：" + emBBrand.getBrandId() +"各字段值："+JSON.toJSONString(emBBrand));
            result.setResult("添加品牌成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加品牌失败!");
        }
        return result;
    }

    @Override
    public Result updateEmBBrand(EmBBrand emBBrand) throws DataAccessException {
        Result result = checkInputEmBBrand(emBBrand);
        if (!result.getCode().equals("200")) return result;
        try {
            brandDao.updateEmBBrand(emBBrand);
            dbLog.info("UPDATE-->EM_B_BRAND-品牌ID：" + emBBrand.getBrandId() +"各字段值："+JSON.toJSONString(emBBrand));
            result.setResult("更新品牌成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "更新品牌失败！");

        }

        return result;
    }

    @Override
    public Result deleteEmBBrand(EmBBrand emBBrand) throws DataAccessException {
        Result result = new Result();
        try {
            brandDao.deleteEmBBrand(emBBrand);
            dbLog.info("DELETE-->EM_B_BRAND-品牌ID：" + emBBrand.getBrandId());
            result.setResult("删除品牌成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除品牌失败！");
        }

        return result;
    }

    @Override
    public Result editMultipleEmBBrandsStatus(String brandIdList) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result deleteMultipleEmBBrands(String brandIdList) throws DataAccessException {
        Result result = new Result();
        try {
            brandDao.deleteMultipleEmBBrands(brandIdList);
            result.setResult("批量删除品牌成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    private Result checkInputEmBBrand(EmBBrand p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getBrandChsName())) {
            result.setCodeResult(Result.CODE_100, "缺少brandChsName中文名称!");
            return result;
        }
        return result;

    }

    @Override
    public List<EmBBrand> searchEmBBrand(EmBBrand emBBrand) throws BaseException {
        List<EmBBrand> list = brandDao.searchEmBBrand(emBBrand);
        return list;

    }
}
